import { protocol, ProtocolRequest } from "electron";
import { join } from "path";
import { URL } from "url";

/**
 * 协议处理
 * @returns
 */
export async function protocolHandle() {
  // 处理 html 访问本地静态文件协议 static:/// --> __static
  protocol.interceptFileProtocol(
    "static",
    (
      request: ProtocolRequest,
      callback: (response: string | Electron.ProtocolResponse) => void
    ) => {
      const url = new URL(request.url);
      const backUrl = join(__static, url.pathname);
      console.log(`重定向 ${url} 至 ${backUrl}`);
      callback({ path: backUrl });
    }
  );
}
